package com.iap.admin.view.filler.code
{
	import com.iap.common.signal.FillerRequestSignal;
	import com.iap.admin.view.code.EditViewCode;
	import com.iap.admin.view.filler.FillerEditView;
	import com.iap.app.util.FormUtils;
	import com.iap.common.model.Filler;
	import com.iap.common.model.Image;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	
	import mx.validators.StringValidator;
	
	public class FillerEditViewCode extends EditViewCode
	{
		public static const CANCEL_NAME:String = "FillerEdit.Cancel";
		public static const SAVE_NAME:String = "FillerEdit.Save";
		public static const DELETE_NAME:String = "FillerEdit.Delete";
		
		public var onSubmit:FillerRequestSignal = new FillerRequestSignal();
		
		private var nameValidator:StringValidator;
		private var view:FillerEditView;
		public var canCloseForm:Boolean = false;
		
		public function FillerEditViewCode()
		{
			super();
		}
		
		protected function initView(view:FillerEditView):void
		{
			this.view = view;
			addListener();
			initValidators();
			resetFocus();
			onReady.dispatch("ready");
		}
		
		private function initValidators():void
		{
			nameValidator = new StringValidator();
			
			nameValidator.source = view.nameForm;
			nameValidator.property = "text";
			nameValidator.required = true;
			nameValidator.maxLength = 100;
			
			validateData.push(nameValidator);
		}
		
		private function addListener():void
		{
			view.nameForm.addEventListener(Event.CHANGE, validateForm);
		}
		
		private function removeListener():void
		{
			view.nameForm.removeEventListener(Event.CHANGE, validateForm);
		}
		
		private function resetFocus():void
		{
		}
		
		private function save(evt:Event = null):void
		{
			if(formIsValid)
				submit();
		}

		protected function submit(closeForm:Boolean = false):void 
		{
			canCloseForm = closeForm;
			model ||= new Filler();
			if((model as Filler).name == null)
				(model as Filler).name = "";
			model = FormUtils.fillObjectFromControls(view, model, FORM_PREFIX);
			setModelType();
			onSubmit.dispatch(model, FillerRequestSignal.SAVE);
		}
		
		protected function remove():void 
		{
			onSubmit.dispatch(model, FillerRequestSignal.DELETE);
		}

		override public function fill(model:Object):void
		{
			super.fill(model);
			setTypeForm();
		}
		
		private function setTypeForm():void
		{
			var selectedType:int = (model as Filler).type;
			switch(selectedType)
			{
				case Filler.TYPE_SOLID:
				{
					view.typeSolidForm.selected = true;
					toggleTypeView(true);
					break;
				}
				case Filler.TYPE_TEXTURE:
				{
					view.typeTextureForm.selected = true;
					toggleTypeView(false);
					view.imageControl.setImage((model as Filler).image);
					break;
				}
			}
		}

		private function setModelType():void
		{
			var selectedType:int = Filler.TYPE_SOLID;
			if(view.typeTextureForm.selected)
			{
				selectedType = Filler.TYPE_TEXTURE;
			}
			(model as Filler).type = selectedType;
		}
		
		public function setImage(item:Image):void
		{
			(model as Filler).image = item;
			submit();
		}
		
		protected function toggleTypeView(visible:Boolean):void
		{
			view.fillerSolid.visible = visible;
			view.fillerSolid.includeInLayout = visible;
			view.fillerTexture.visible = !visible;
			view.fillerTexture.includeInLayout = !visible;
		}
	}
}